# Copyright 2006 Michael Gorsuch <michael@styledbits.com>

# This file is part of GrabTheMic <http://www.grabthemic.org>.

# GrabTheMic is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.

#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.

#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


class StoryController < ApplicationController
  
  before_filter :authorize, :only => [:new, :create, :yea, :nay, :remove_vote]
  before_filter :set_user
  layout 'default', :except => [:rss]
  
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :create ],
         :redirect_to => { :action => :list }

  def list
    if @user
      @votes = {}
      @user.votes.find(:all, :select => "story_id, value").each {|v| @votes[v.story_id] = v.value}
    end
    
    if params[:order] == 'top'
      @story_pages, @stories = paginate :stories, :per_page => 10, :order => "points DESC,created_on DESC"
    else
      @story_pages, @stories = paginate :stories, :per_page => 10, :order => "created_on DESC"
    end
  end

  def new
    @story = Story.new
    @story.url = params[:url] || nil
    @story.title = params[:title] || nil
  end

  def create
    @story = Story.new(params[:story])
    @story.user = @user
    if @story.save
      @story.vote_up @user
      AlertMailer.deliver_new_story @story
      flash[:notice] = "<em>#{@story.title}</em> was successfully created."
      if params[:go_back] == 'true'
        redirect_to @story.url  
      else
        redirect_to :action => 'list'
      end
    else
      render :action => 'new'
    end
  end
  
  def yea
    story = Story.find(params[:id])
    story.vote_up @user
    if params[:go_back]
      redirect_to params[:go_back]  
    else  
      redirect_to :action => "list"
    end
  end
  
  def nay
    story = Story.find(params[:id])
    story.vote_down @user
    if params[:go_back]
      redirect_to params[:go_back]  
    else  
      redirect_to :action => "list"
    end
  end
  
  def remove_vote
    story = Story.find(params[:id])
    story.remove_vote @user
    if params[:go_back]
      redirect_to params[:go_back]  
    else  
      redirect_to :action => "list"
    end
  end
  
  def rss
    @stories = Story.find(:all, :limit => 10, :order => "created_on DESC")
  end
  
  def goto 
    story = Story.find(params[:id])
    redirect_to story.url
  end
  
end
